package com.e2oto.seller.modules.launch.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.e2oto.seller.R;
import com.e2oto.seller.common.base.BaseMvpActivity;
import com.e2oto.seller.common.utils.LogTools;
import com.e2oto.seller.common.utils.RegexTools;
import com.e2oto.seller.common.utils.SPtools;
import com.e2oto.seller.common.utils.ToastUtils;
import com.e2oto.seller.modules.SellerApp;
import com.e2oto.seller.modules.launch.bean.CodeBean;
import com.e2oto.seller.modules.launch.bean.LoginBean;
import com.e2oto.seller.modules.launch.presenter.RegisterPresenter;
import com.e2oto.seller.modules.launch.views.RegisterView;
import com.e2oto.seller.modules.seller.ui.HomeActivity;
import com.e2oto.seller.modules.seller.ui.ShopBaseInfoActivity;
import com.e2oto.seller.widget.LoginEditText;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class RegisterActivity extends BaseMvpActivity<RegisterView, RegisterPresenter> implements RegisterView {

    @BindView(R.id.register_phone_et)
    LoginEditText phoneEt;
    @BindView(R.id.register_password_et)
    LoginEditText passwordEt;
    @BindView(R.id.register_get_code)
    TextView sendCodeTv;
    @BindView(R.id.register_code_et)
    EditText codeEt;

    @Override
    protected void afterView(Bundle savedInstanceState) {
        super.afterView(savedInstanceState);
    }

    @Override
    protected RegisterPresenter createPresenter() {
        return new RegisterPresenter();
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_register;
    }

    @Override
    public void showMessage(String msg) {
        ToastUtils.INSTANCE.showToast(this, msg);
    }

    @OnClick({R.id.toolbar_left_btn, R.id.register_get_code, R.id.register_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left_btn:
                finish();
                break;
            case R.id.register_get_code:
                String phone = phoneEt.getEditText().getText().toString().trim();
                if (!RegexTools.isMobileSimple(phone)) {
                    showMessage("请输入正确的手机号码");
                    return;
                }
                mPresenter.sendCode(phone);
                break;
            case R.id.register_btn:
                String mobile = phoneEt.getEditText().getText().toString().trim();
                String code = codeEt.getText().toString().trim();
                String password = passwordEt.getEditText().getText().toString().trim();
                if (!RegexTools.isMobileSimple(mobile)) {
                    showMessage("请输入正确的手机号码");
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    showMessage("请输入验证码");
                    return;
                }
                if (code.length() != 4) {
                    showMessage("请输入4位数字的验证码");
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    showMessage("请输入您的密码");
                    return;
                }
                if (password.length() < 6) {
                    showMessage("密码长度不能小于6位数");
                    return;
                }
                mPresenter.register(mobile, code, password);
                break;
        }
    }

    @Override
    public void sendCodeCallback(CodeBean codeBean) {
        LogTools.e("register:" + codeBean.getCode());
        final int count = 60;
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(count)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(@NonNull Long aLong) throws Exception {
                        return count - aLong;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        sendCodeTv.setTextColor(ContextCompat.getColor(RegisterActivity.this, R.color.theme_color));
                        sendCodeTv.setClickable(false);
                    }

                    @Override
                    public void onNext(Long aLong) {
                        sendCodeTv.setText("验证码已发送" + aLong);
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                        sendCodeTv.setTextColor(ContextCompat.getColor(RegisterActivity.this, R.color.black));
                        sendCodeTv.setClickable(true);
                        sendCodeTv.setText("获取验证码");
                    }
                });
    }

    @Override
    public void registerCallback(LoginBean loginBean) {
        SellerApp.userId = loginBean.getUser_id() + "";
        SellerApp.sellerId = loginBean.getSeller_id() + "";
        SPtools.put(this, "category_id", loginBean.getCategory_id());
        SPtools.put(this, "batch_category_id", loginBean.getBatch_category_id());

        if (loginBean.getSeller_status() == 0) {
            startActivity(new Intent(this, ShopBaseInfoActivity.class));
        } else if (loginBean.getSeller_status() == 1) {
            ToastUtils.INSTANCE.showToast(this, "您提交的入驻申请正在审核中");
            startActivity(new Intent(this, HomeActivity.class));
        } else {
            startActivity(new Intent(this, HomeActivity.class));
        }
        exitApp();
    }


}
